using System;
using System.IO;
using SpecMaker.Core.Interfaces;
using System.Linq;
namespace SpecMaker.Core
{
    public class CommandLineClient
    {
        private readonly IOutputWrapper _wrapper;
        private readonly WindsorAppConfig _appconfig;

        public CommandLineClient(IOutputWrapper wrapper, WindsorAppConfig appconfig)
        {
            _wrapper = wrapper;
            _appconfig = appconfig;
        }

        public void Run(string[] args)
        {
            var arglist = args.ToList();
            if(arglist.Count==0)
            { 
                _wrapper.WriteLine("usage: specmaker.exe <Assembly Location> -report <HTML|XML|CONSOLE>");
                _wrapper.WriteLine("-report is optional and defaults to a CONSOLE report");
            }
            else
            {
                if(arglist.Count(x=>x =="-report")>0)
                {
                    var index = arglist.FindIndex(x => x == "-report");
                    var reporttype = arglist[index + 1].ToUpper();
                    if (reporttype == "CONSOLE")
                        _appconfig.InitializeRunner(typeof(ConsoleReportFactory)).Execute(AppPath() + "\\" + arglist[0]);
                    else
                    {
                        _appconfig.InitializeRunner(typeof(HtmlReporter)).Execute(AppPath()+"\\" + arglist[0]);
                    }

                }
                else
                {
                    _appconfig.InitializeRunner(typeof(ConsoleReportFactory)).Execute(AppPath() +"\\" +arglist[0]);
                }
            }
        }
        private string AppPath()
        {
            return Directory.GetCurrentDirectory();
        }
    }
}